#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include "CubicPPSplines.h" // 确保这是你的 PP 样条头文件
#include "CubicBSplines.h"  // 确保这是你的 B 样条头文件

// 定义一个样本函数
double sampleFunction(double x) {
    return sin(x) ;
}

int main() {
    // 生成样本点
    std::vector<double> x, y;
    int numPoints = 150;
    double start = 0,end = 2 * M_PI;
    for (int i = 0; i < numPoints; ++i) {
        double xi = start + (end - start) * i / (numPoints - 1);
        x.push_back(xi);
        y.push_back(sampleFunction(xi));
    }

    // 创建三种 PP 样条和 B 样条
    CubicPPSplinescomplete completeSpline_pp(x, y, 1, -1);  // 假设端点导数为 1 和 -1
    CubicPPSplinesnatural naturalSpline_pp(x, y, 1, -1);     // 自然样条，端点二阶导为 0
    CubicPPSplinesperiodic periodicSpline_pp(x, y);   // 周期样条，假设端点导数和二阶导为 0
    CubicBSpline_complete completeSpline_b(x, y, 1, -1);    // 假设端点导数为 1 和 -1
    CubicBSpline_natural naturalSpline_b(x, y);             // 自然样条，端点二阶导为 0
    CubicBSpline_periodic periodicSpline_b(x, y);           // 周期样条，假设端点导数和二阶导为 0

    // 生成更密集的点用于绘图
    std::ofstream outFile("spline_data.txt");
    int numDensePoints = 200;
    for (int i = 0.5; i < numDensePoints; ++i) {
        double xi = start + (end - start) * i / (numDensePoints - 1);
        double y_true = sampleFunction(xi);
        double y_complete_pp = completeSpline_pp.value(xi);
        double y_natural_pp = naturalSpline_pp.value(xi);
        double y_periodic_pp = periodicSpline_pp.value(xi);
        double y_complete_b = completeSpline_b.B_value(xi);
        double y_natural_b = naturalSpline_b.B_value(xi);
        double y_periodic_b = periodicSpline_b.B_value(xi);
        outFile << xi << " " << y_true << " " << y_complete_pp << " " << y_natural_pp << " " << y_periodic_pp << " "
                << y_complete_b << " " << y_natural_b << " " << y_periodic_b << std::endl;
    }
    outFile.close();

    // 创建 gnuplot 脚本
    std::ofstream scriptFile("plot_splines.gp");
    scriptFile << "set terminal pngcairo enhanced color size 1200,800\n";
    scriptFile << "set output 'spline_comparison.png'\n";
    scriptFile << "set title 'Comparison of PP and B Splines'\n";
    scriptFile << "set xlabel 'x'\n";
    scriptFile << "set ylabel 'y'\n";
    scriptFile << "set key outside\n";
    scriptFile << "plot 'spline_data.txt' using 1:2 with lines title 'True Function', \\\n";
    scriptFile << "     '' using 1:3 with lines title 'Complete PP Spline', \\\n";
    scriptFile << "     '' using 1:4 with lines title 'Natural PP Spline', \\\n";
    scriptFile << "     '' using 1:5 with lines title 'Periodic PP Spline', \\\n";
    scriptFile << "     '' using 1:6 with lines title 'Complete B Spline', \\\n";
    scriptFile << "     '' using 1:7 with lines title 'Natural B Spline', \\\n";
    scriptFile << "     '' using 1:8 with lines title 'Periodic B Spline'\n";
    scriptFile.close();

    // 运行 gnuplot 脚本
    system("gnuplot plot_splines.gp");

    std::cout << "Plot saved as 'spline_comparison.png'" << std::endl;

    return 0;
}
